package com.cea.papyrustest.uml.deletecommand;

import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Generalization;

import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.commands.UMLElementDeleteCommand;

/**
 * Test class for {@link Generalization} creation command
 * @author Patrick Tessier
 */
public class GeneralizationDeleteCommandTest extends PapyrusCommandTest{

	protected Class theParent;
	/**
	 * Default constructor
	 * @param name the name oif the test case
	 */
	public GeneralizationDeleteCommandTest(String name) {
		super(name);	
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		// search class_0 with generalization
		papyrusCommand= new UMLElementCreateCommand(org.eclipse.uml2.uml.Class.class,model);
		stack.execute(papyrusCommand);
		theParent=(Class)((UMLElementCreateCommand)papyrusCommand).getElement();
		
		papyrusCommand= new UMLElementCreateCommand(org.eclipse.uml2.uml.Generalization.class,theParent);
		stack.execute(papyrusCommand);
		Generalization generalization =(Generalization)((UMLElementCreateCommand)papyrusCommand).getElement();
		if(generalization == null) {
			fail("impossible to create a generalization");
		}
		
		papyrusCommand = new UMLElementDeleteCommand(generalization, theParent);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrustest.PapyrusCommandTest#testExecute()
	 */
	@Override
	public void testExecute() {
		stack.execute(papyrusCommand);
		assertEquals("the extension exist!", 0, theParent.getGeneralizations().size());

		stack.undo();
		Element clazz=theParent.getGeneralizations().get(0);
		assertNotNull("the generalization is not re-created",clazz);
		assertTrue("this is not an generalization",((clazz)instanceof Generalization));
		
		stack.redo();
		assertEquals("the extension exist!", 0, theParent.getGeneralizations().size());
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrustest.uml.deletecommand.PapyrusCommandTest#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		//delete class
		papyrusCommand = new UMLElementDeleteCommand(theParent, theParent.getOwner());
		papyrusCommand.execute();
	}
}
